% GAUTIER LE BIHAN - 2020
% Replication files for "Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector 
% Menu-Cost Model ?" Review of Economics and Statistics
%
% Estimation of Aggregate 4-sector CP model 
% inputs for IRF Table 4

addpath('..\..\Utilities')  
load actual_moments_k

load res_estim_prodc4n
param0=res_estim_prodc4n


actual_moments=[    0.1308	    0.6215	    0.0439	    0.0813	    4.1453;
                    0.0738	    0.6487	    0.0545	    0.0877	    4.8384;
                    0.0589	    0.8652	    0.0475	    0.0555	    7.2764;
                    0.6812	    0.6110	    0.0314	    0.0581	    2.8919];
		

actual_std=[    0.0011	 ;   0.0042	  ;  0.0004	  ;  0.0009	  ;  0.0876;
                0.0009	 ;   0.0063	  ;  0.0009	  ;  0.0028	  ;  0.1885;
                0.0009	 ;   0.0054	  ;  0.0009	 ;   0.0016	  ;  0.4870;
                0.0009	 ;   0.0054	  ;  0.0009	 ;   0.0016	  ;  0.4870];
                %0.0012	 ;   0.0021	 ;   0.0001	 ;   0.0003	  ;  0.0254];
 actual_var= actual_std.* actual_std*1000;


 for jj=1;

   p01 =param0(1);
   p02 =param0(2);
   p03 =param0(3);
   p04 =param0(4);
   
    mu_c01 =param0(5);
    mu_c02 =param0(6);
    mu_c03 =param0(7);
    mu_c04 =param0(8);
    
    sig_eps_a01=param0(9);
    sig_eps_a02=param0(10);
    sig_eps_a03=log(exp(param0(11)));
    sig_eps_a04=param0(12);
    
weight_j=[0.153; 0.179; 0.188; 0.05];


% Adjustement of initial search area (beyond 5 percent)

power_p0 = 1.0;
power_mu_c = 1.;
power_sig_eps_a = 1.;
power_rho_a = 1;

% save SMM

% create vector for initial parameter values
vec0 = [p01; p02; p03; p04; mu_c01; mu_c02;mu_c03;mu_c04; sig_eps_a01; sig_eps_a02;sig_eps_a03; sig_eps_a04]

% Keep track of results
conv_SMM = zeros(0,3);
xopt_SMM = 100000; % start iteration
conv_opt = zeros(3,6);
SMM_count = 0; % counter

% tolerance level
tol_SMM = 5e-3;
scale=[actual_var];


save('SMM','vec0','p01','p02','p03','p04','mu_c01','mu_c02','mu_c03','mu_c04','sig_eps_a01','sig_eps_a02','sig_eps_a03','sig_eps_a04',...
        'power_p0','power_mu_c','power_sig_eps_a','power_rho_a',...
    'conv_SMM','xopt_SMM','conv_opt','SMM_count',...
    'tol_SMM','-append', 'weight_j')
% 'power_phi','phi_init','phi0'

%% Options
%  Display, TolX, TolFun, MaxFunEvals

%% Set calibration target
% std.Y, autocorr(Y), frequency of default:
moment1 = actual_moments(1,1);
moment2 = actual_moments(1,2);
moment3 = actual_moments(1,3);
moment4 = actual_moments(1,4); 
moment5 = actual_moments(1,5);

moment6 = actual_moments(2,1);
moment7 = actual_moments(2,2);
moment8 = actual_moments(2,3);
moment9 = actual_moments(2,4); 
moment10 = actual_moments(2,5);

moment11 = actual_moments(3,1);
moment12 = actual_moments(3,2);
moment13 = actual_moments(3,3);
moment14 = actual_moments(3,4); 
moment15 = actual_moments(3,5);


moment16 = actual_moments(4,1);
moment17 = actual_moments(4,2);
moment18 = actual_moments(4,3);
moment19 = actual_moments(4,4); 
moment20 = actual_moments(4,5);

target = [moment1; moment2; moment3; moment4; moment5; moment6; moment7; moment8; moment9; moment10;
    moment11; moment12; moment13; moment14; moment15; moment16; moment17; moment18; moment19; moment20];
save('SMM','target', 'scale','-append')
%% Start minimization routine 

% options = optimset('MaxIter',30);%, 'TolX', 1e-4,'Tolfun', 1e-3);
options = optimset('TolX', 1e-4,'Tolfun', 1e-3);
options = optimset('MaxFunEvals', 120);

[x,fval, exiflag] = fminsearch(@SMMmain,vec0, options);
res_estim_prodc4n(jj,:)=[x' fval exiflag target' ]

 
  save res_estim_prodc4n res_estim_prodc4n ;

end